package com.example.xiaoyuan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xiaoyuan.pojo.Grade;
import com.example.xiaoyuan.service.GradeService;
import com.example.xiaoyuan.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    /**
     * 获取年级列表
     * @return
     */
    @GetMapping("/getGrades")
    public Result getGrades (){

        List<Grade> gradeList = gradeService.getGrades();
        return Result.ok(gradeList);
    }

    @ApiOperation("删除年级信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(@RequestBody List<Integer> ids){
        gradeService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 年级新增和修改
     * @param grade
     * @return
     */
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@RequestBody Grade grade){

        gradeService.saveOrUpdate(grade);

        return Result.ok();
    }

    /**
     * 分页查询年级列表
     * @param pageNo
     * @param pageSize
     * @param gradeName
     * @return
     */
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGradse(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            String gradeName){

        // 分页带条件查询
        Page<Grade> pageParam = new Page<>(pageNo, pageSize);

        //通过服务层
        IPage<Grade> gradeIPage = gradeService.getGradeByopr(pageParam, gradeName);

        return Result.ok(gradeIPage);
    }
}
